package com.kaiyidiannao.activity.SecondHandHouse;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
import com.kaiyidiannao.activity.login_or_registered.LoginOrRegistered;
import com.kaiyidiannao.activity.user_detail.EditUserDetail;
import com.kaiyidiannao.adapter.SecondCarCommonAdapter;
import com.kaiyidiannao.adapter.SecondHandHouseAdapter;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.common.pulltorefresh_view.DragListView;
import com.kaiyidiannao.common.util.CharacterParser;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.SecondHandHouse;
import com.kaiyidiannao.model.bean.UserBean;
import com.kaiyidiannao.model.req.model.GetSecondHandHouseLIst;
import com.kaiyidiannao.model.req.model.GetSecondHandHouseLIstTop;
import com.kaiyidiannao.model.req.model.GetSecondHandHouseSearch;
import com.kaiyidiannao.view.ActionBarView;
import com.kaiyidiannao.view.AutoTextView;
import com.taplinker.core.util.LogUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;


/**
 * Created by mark on 2014/4/11.
 */
public class SecondHandHouseActivity extends BaseActionBarActivity implements Observer {

    private TextView secondHandHousePrie;
    public CharacterParser characterParser;
    private SecondCarCommonAdapter adapter;
    private ArrayList<UserBean> userBeans = DataCenter.userLogin;
    private ArrayList<UserBean> beans = DataCenter.userBeans;
    private LinearLayout linearLayout;//根view
    private AutoTextView mTextView02;
    private SecondHandHouseAdapter secondHandHouseAdapter = null;
    private ArrayList<String> list1 = new ArrayList<String>();
    private ArrayList<String> list2 = new ArrayList<String>();

    TextView secondHandHouseAge, secondHandHouseArea, secondHandHousePrice;
    EditText search;
    private ProgressBar progressBar;
    private List<SecondHandHouse> secondHandHouseLists = DataCenter.secondHandHouse;
    private DragListView secondHandHouseList;
    FragmentActivity secondHandHouseActivity = null;

    private View no_content_view;//没有数据的布局对象
    private ListView lv_house;
    private RelativeLayout rl_house;

    //请求数据参数
    private int catid = DataCenter.secondHandHouse_category_id;
    private int currentPage = DataCenter.LISTVIEW_DEFAULT_PAGE;
    int page = DataCenter.LISTVIEW_DEFAULT_PAGE;
    private boolean flag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondhandhouse);
        DataCenter.rolling_current_module_id = DataCenter.moduleFlag.SecondHandHouse;
        findActionBar();
        initView();
        findView();
        DataCenter.getInstance().addObserver(this);
        actionBarView.button_right_icon2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (userBeans.size() == 0) {
                    Intent intent = new Intent(SecondHandHouseActivity.this, LoginOrRegistered.class);
                    intent.putExtra("ClassName", SecondHandHouseActivity.class.getName());
                    startActivity(intent);
                    Toast.makeText(SecondHandHouseActivity.this, "请先登录", Toast.LENGTH_SHORT).show();
                    //  finish();
                } else if (beans.get(0).getUsername() == null || beans.get(0).getUsername().equals("")) {
                    Intent intent = new Intent(SecondHandHouseActivity.this, EditUserDetail.class);
                    intent.putExtra("ClassName", SecondHandHouseActivity.class.getName());
                    startActivity(intent);
                    Toast.makeText(SecondHandHouseActivity.this, "请完善您的个人信息", Toast.LENGTH_SHORT).show();
                    // finish();
                } else {
                    Log.d("username", beans.get(0).getUserid() + "----------");
                    Intent intent = new Intent(SecondHandHouseActivity.this, SecondHandHousePublish.class);
                    startActivity(intent);
                }
            }
        });
        secondHandHouseList.setVisibility(View.VISIBLE);
        mTextView02.setVisibility(View.VISIBLE);
        progressBar.setVisibility(View.VISIBLE);
        new GetSecondHandHouseLIst(catid).execute();
        if (!HttpUtils.isConnected()) {
        } else {
            new GetSecondHandHouseLIstTop(catid).execute();
        }
    }

    @Override
    protected void onRestart() {
        if (flag) {
            new GetSecondHandHouseSearch(DataCenter.index5, DataCenter.index4, DataCenter.search, currentPage).execute();
        } else {
            new GetSecondHandHouseLIst(catid, currentPage).execute();
        }
        super.onRestart();
    }

    private void findActionBar() {
        actionBarView = (ActionBarView) findViewById(R.id.actionbar);
        actionBarView.setTitle("二手房");
        actionBarView.button_right_icon2.setVisibility(View.VISIBLE);
        actionBarView.button_right_icon2.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.publish));

    }



    private void initView() {
        no_content_view = findViewById(R.id.no_content_view);
        secondHandHouseList = (DragListView) findViewById(R.id.secondHandHouseList);
        secondHandHouseAdapter = new SecondHandHouseAdapter(this, secondHandHouseLists);
        secondHandHouseList.setAdapter(secondHandHouseAdapter);
        secondHandHouseAdapter.notifyDataSetChanged();
        mTextView02 = (AutoTextView) findViewById(R.id.switcher02);
        progressBar = (ProgressBar) findViewById(R.id.second_inner_progress_bar);
        secondHandHouseAge = (TextView) findViewById(R.id.secondHandHouseAge);
        secondHandHouseArea = (TextView) findViewById(R.id.secondHandHouseArea);
        secondHandHousePrie = (TextView) findViewById(R.id.secondHandHousePrie);
        lv_house = (ListView) findViewById(R.id.lv_house);
        rl_house = (RelativeLayout) findViewById(R.id.rl_house);
        secondHandHouseAge.setBackgroundDrawable(getResources().getDrawable(R.drawable.triangle_light));
        secondHandHouseAge.setTextColor(Color.rgb(245, 142, 111));

        secondHandHouseAge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (DataCenter.flags2 == 1) {
                    lv_house.setVisibility(View.GONE);
                    rl_house.setVisibility(View.VISIBLE);
                    mTextView02.setVisibility(View.VISIBLE);
                    DataCenter.flags2 = -1;
                } else {
                    AlertDialog.Builder dialog = new AlertDialog.Builder(SecondHandHouseActivity.this);
                    LayoutInflater inflater = (LayoutInflater) SecondHandHouseActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.edittext, null);
                    dialog.setView(layout);
                    search = (EditText) layout.findViewById(R.id.editText);
                    dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            String searchC = search.getText().toString();
                            if(searchC.contains(" ")){
                                showToast("不能输入空格");
                                return;
                            }else if(searchC.length()>20){
                                showToast("小区名称不大大于20位");
                                return;
                            }
                            else{
                                DataCenter.search = searchC;
                                new GetSecondHandHouseSearch(DataCenter.index5, DataCenter.index4, DataCenter.search, DataCenter.LISTVIEW_DEFAULT_PAGE).execute();
                                progressBar.setVisibility(View.VISIBLE);
                            }
                        }
                    });
                    dialog.setTitle("请输入小区名称");
                    dialog.show();
//                    if(search.getText().toString().contains(" ")){
//
//                    }

                    lv_house.setVisibility(View.GONE);
                    rl_house.setVisibility(View.VISIBLE);
                    secondHandHouseAge.setBackgroundDrawable(getResources().getDrawable(R.drawable.triangle_light));
                    secondHandHouseArea.setBackgroundDrawable(getResources().getDrawable(R.drawable.triangle_dark));
                    secondHandHousePrie.setBackgroundDrawable(getResources().getDrawable(R.drawable.triangle_dark));
                    secondHandHouseAge.setTextColor(Color.rgb(245, 142, 111));
                    secondHandHouseArea.setTextColor(Color.rgb(0, 0, 0));
                    secondHandHousePrie.setTextColor(Color.rgb(0, 0, 0));
                    DataCenter.flags1 = 1;
                }

            }
        });

        secondHandHouseArea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (DataCenter.flags2 == 2) {
                    lv_house.setVisibility(View.GONE);
                    rl_house.setVisibility(View.VISIBLE);
                    mTextView02.setVisibility(View.VISIBLE);
                    DataCenter.flags2 = -1;
                } else {
                    lv_house.setVisibility(View.VISIBLE);
                    rl_house.setVisibility(View.GONE);
                    mTextView02.setVisibility(View.GONE);
                    list1.clear();
                    initViewForArea();
//                    adapter = new SecondCarCommonAdapter(SecondHandHouseActivity.this, R.layout.sift_house_other_item, R.id.itemTv, list1);//area 2 list1
                    DataCenter.lmap.clear();
                    adapter = new SecondCarCommonAdapter(SecondHandHouseActivity.this,list1);
                    lv_house.setAdapter(adapter);
                    secondHandHouseAge.setBackgroundDrawable(getResources().getDrawable(R.drawable.triangle_dark));
                    secondHandHouseArea.setBackgroundDrawable(getResources().getDrawable(R.drawable.triangle_light));
                    secondHandHousePrie.setBackgroundDrawable(getResources().getDrawable(R.drawable.triangle_dark));
                    secondHandHouseArea.setTextColor(Color.rgb(245, 142, 111));
                    secondHandHouseAge.setTextColor(Color.rgb(0, 0, 0));
                    secondHandHousePrie.setTextColor(Color.rgb(0, 0, 0));
                    DataCenter.flags2 = 2;
                }

            }
        });
        secondHandHousePrie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (DataCenter.flags2 == 3) {
                    lv_house.setVisibility(View.GONE);
                    rl_house.setVisibility(View.VISIBLE);
                    mTextView02.setVisibility(View.VISIBLE);
                    DataCenter.flags2 = -1;
                } else {
                    lv_house.setVisibility(View.VISIBLE);
                    rl_house.setVisibility(View.GONE);
                    mTextView02.setVisibility(View.GONE);
                    list2.clear();
                    initViewForPrice();
//                    adapter = new ArrayAdapter(SecondHandHouseActivity.this, R.layout.sift_house_other_item, R.id.itemTv, list2);//area 2 list1
                    DataCenter.lmap.clear();
                    adapter = new SecondCarCommonAdapter(SecondHandHouseActivity.this,list2);
                    lv_house.setAdapter(adapter);
                    secondHandHouseAge.setBackgroundDrawable(getResources().getDrawable(R.drawable.triangle_dark));
                    secondHandHouseArea.setBackgroundDrawable(getResources().getDrawable(R.drawable.triangle_dark));
                    secondHandHousePrie.setBackgroundDrawable(getResources().getDrawable(R.drawable.triangle_light));
                    secondHandHousePrie.setTextColor(Color.rgb(245, 142, 111));
                    secondHandHouseArea.setTextColor(Color.rgb(0, 0, 0));
                    secondHandHouseAge.setTextColor(Color.rgb(0, 0, 0));
                    DataCenter.flags2 = 3;
                }

            }
        });

        lv_house.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (DataCenter.flags2 == 2) {
                    DataCenter.index4 = position;
                    TextView textview = null;
                    ImageView imageView = null;
                    //对ListView中的ChildView进行操纵。。。
                    textview = (TextView) DataCenter.lmap.get(DataCenter.index4).findViewById(R.id.itemTv);
                    imageView = (ImageView) DataCenter.lmap.get(DataCenter.index4).findViewById(R.id.itemIv);
                    textview.setTextColor(getResources().getColor(R.color.title_detail_color));
                    imageView.setVisibility(View.VISIBLE);
                    secondHandHouseArea.setText(list1.get(position));
                }
                if (DataCenter.flags2 == 3) {
                    DataCenter.index5 = position;
                    TextView textview = null;
                    ImageView imageView = null;
                    //对ListView中的ChildView进行操纵。。。
                    textview = (TextView) DataCenter.lmap.get(DataCenter.index5).findViewById(R.id.itemTv);
                    imageView = (ImageView) DataCenter.lmap.get(DataCenter.index5).findViewById(R.id.itemIv);
                    textview.setTextColor(getResources().getColor(R.color.title_detail_color));
                    imageView.setVisibility(View.VISIBLE);
                    secondHandHousePrie.setText(list2.get(position));
                }
                lv_house.setVisibility(View.GONE);
                rl_house.setVisibility(View.VISIBLE);
                mTextView02.setVisibility(View.VISIBLE);
                if(DataCenter.search == null){
                    DataCenter.search = "";
                }
                progressBar.setVisibility(View.VISIBLE);
                new GetSecondHandHouseSearch(DataCenter.index5, DataCenter.index4, DataCenter.search, DataCenter.LISTVIEW_DEFAULT_PAGE).execute();
                flag = true;
            }
        });


    }

    public void findView() {
        secondHandHouseList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(SecondHandHouseActivity.this, SecondHandHouseDetail.class);
                Bundle bundle = new Bundle();
                intent.putExtra("catid", secondHandHouseLists.get(position - 1).getCatid());
                intent.putExtra("itemid", secondHandHouseLists.get(position - 1).getItemid());
                intent.putExtra("itemid", secondHandHouseLists.get(position - 1).getItemid());
                intent.putExtra("catid", secondHandHouseLists.get(position - 1).getCatid());
                intent.putExtra("addtime", secondHandHouseLists.get(position - 1).getAddtime());
                intent.putExtras(bundle);
                startActivity(intent);
                DataCenter.house_position = position-1;
                DataCenter.PAGE = (position-1)/10+1;
                currentPage = (position-1)/10+1;
            }
        });
        secondHandHouseList.setOnRefreshListener(new DragListView.OnRefreshLoadingMoreListener() {
            @Override
            public void onRefresh() {
                progressBar.setVisibility(View.VISIBLE);
                if (flag) {
                    new GetSecondHandHouseSearch(DataCenter.index5, DataCenter.index4, DataCenter.search, page).execute();
                    page = DataCenter.LISTVIEW_DEFAULT_PAGE;
                } else {
                    new GetSecondHandHouseLIst(catid, DataCenter.LISTVIEW_DEFAULT_PAGE).execute();
                    currentPage = DataCenter.LISTVIEW_DEFAULT_PAGE;
                }
            }

            @Override
            public void onLoadMore() {
                progressBar.setVisibility(View.VISIBLE);
                if (flag) {
                    new GetSecondHandHouseSearch(DataCenter.index5, DataCenter.index4, DataCenter.search, ++currentPage).execute();
                } else {
                    new GetSecondHandHouseLIst(catid, ++currentPage).execute();
                }
            }
        });


    }

    @Override
    public void update(Observable observable, Object data) {
        DataCenter.flag flag = (DataCenter.flag) data;
        DataCenter.isLoad = true;
        if (!HttpUtils.isConnected()) {
            progressBar.setVisibility(View.GONE);
            no_content_view.setVisibility(View.VISIBLE);
            secondHandHouseList.onRefreshComplete();
            secondHandHouseList.onLoadMoreComplete(false);
        }

        if (flag == DataCenter.flag.GET_SECOND_HAND_HOUSE_LIST_SUCCESS
                || flag == DataCenter.flag.GET_SECOND_HAND_HOUSE_FAILED) { //类表数据
            // secondHandHouseLists.addAll(DataCenter.secondHandHouse);
            progressBar.setVisibility(View.GONE);
            secondHandHouseList.setVisibility(View.VISIBLE);
            secondHandHouseList.onRefreshComplete();
            secondHandHouseList.onLoadMoreComplete(false);
            secondHandHouseAdapter.notifyDataSetChanged();


            if (flag == DataCenter.flag.GET_SECOND_HAND_HOUSE_LIST_SUCCESS) {

                // Log.d(DataCenter.LOG_INFO, " DdcListActivity.update() -> SUCCESS");
                if ((secondHandHouseLists == null || secondHandHouseLists.size() < 1)) {
                    no_content_view.setVisibility(View.VISIBLE);
                    secondHandHouseList.setVisibility(View.INVISIBLE);
                    ((TextView) no_content_view.findViewById(R.id.notice_msg_tv)).setText(getText(R.string.listview_msg_empty));
                } else {
                    secondHandHouseList.setVisibility(View.VISIBLE);
                    no_content_view.setVisibility(View.GONE);
                }
            }

            if (flag == DataCenter.flag.GET_SECOND_HAND_HOUSE_FAILED) {
                if ((secondHandHouseLists == null || secondHandHouseLists.size() < 1)) {
                    no_content_view.setVisibility(View.VISIBLE);
                    secondHandHouseList.setVisibility(View.INVISIBLE);
                    ((TextView) no_content_view.findViewById(R.id.notice_msg_tv)).setText(getText(R.string.listview_msg_error));
                } else {
                    no_content_view.setVisibility(View.GONE);
                }
            }
        } else { // 置顶滚动栏数据
            ArrayList<SecondHandHouse> topBeanList = DataCenter.secondHandHouseTop;
            mTextView02.setData3(topBeanList);
            mTextView02.startRolling();
        }

    }

    private void initTag() {
        secondHandHouseAge.setBackgroundDrawable(getResources().getDrawable(R.drawable.triangle_dark));
        secondHandHouseArea.setBackgroundDrawable(getResources().getDrawable(R.drawable.triangle_dark));
        secondHandHousePrie.setBackgroundDrawable(getResources().getDrawable(R.drawable.triangle_dark));
    }

    /**
     * 面积筛选页面的初始化
     */
    private void initViewForArea() {
        list1.add("不限");
        list1.add("50㎡以下");
        list1.add("50-70㎡");
        list1.add("70-90㎡");
        list1.add("90-110㎡");
        list1.add("110-130㎡");
        list1.add("130-150㎡");
        list1.add("150-200㎡");
        list1.add("200-300㎡");
        list1.add("300㎡以上");
    }

    /**
     * 价格筛选页面的初始化
     */
    private void initViewForPrice() {
        list2.add("不限");
        list2.add("20万以下");
        list2.add("20-50万");
        list2.add("50-100万");
        list2.add("100-150万");
        list2.add("150万以上");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.d(DataCenter.LOG_INFO, secondHandHouseActivity + ".onDestroy");
        DataCenter.secondHandHouseTop.clear();
        DataCenter.search = null;
        DataCenter.index4 = 0;
        DataCenter.index5 = 0;
        DataCenter.flags2 = -1;
        //删除观察者
        DataCenter.getInstance().deleteObserver(this);
    }

}
